<?php
namespace app\api\controller\xiservice;

use app\common\controller\xiservice\XiserviceApi;
use app\common\model\xiservice\Schedule as ScheduleModel;
use app\common\model\xiservice\Service as ServiceModel;
use app\common\model\xiservice\User;
use app\common\model\xiservice\Timelist;
use app\common\model\xiservice\Order as OrderModel;
use think\Db;
/**
 * 排班模块
 */
class Schedule extends XiserviceApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];



    /**
     * @ApiTitle (服务人员日期排班信息)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.schedule/list)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams   (name="date", type="string", description="日期", sample="2022-05-18")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','start_time':'string','end_time':'string','checked':'int'}", description="返回数据")
     */
    public function list() {
        $date = $this->request->request('date');  
              
        $worker =   User::where('user_id',$this->auth->id)->find();
        if(!$worker){
            $this->error('服务人员不存在!');
        } 
        if(!$worker['work_start_time']){
            $this->error('暂未设置工作时间!');
        }     

        $list = ScheduleModel::where('user_id',$this->auth->id)
                        ->where('date',$date)
                        ->select();  

        $Timelist=Timelist::where('start_time','>=',$worker['work_start_time'])->where('end_time','<=',$worker['work_end_time'])->order('id asc')->select();
            foreach ($Timelist as $row) {
                $row['checked'] = 1;
                $row['date'] = $date;
                if($list){
                    foreach ($list as $rows) {
                        if($row['start_time']==$rows['rest_start_time']){
                             $row['checked'] = 0;
                        }
                    }
                }
            }
        $this->success('查询成功',$Timelist);
    }

    /**
     * @ApiTitle (设置日期排班休息)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.schedule/set)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams   (name="date", type="string", description="日期", sample="2022-05-18")
     * @ApiParams   (name="ids", type="string", description="时间段id", sample="34,35,36")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'int','start_time':'string','end_time':'string'}", description="返回数据")
     */
    public function set() {
        $date = $this->request->request('date');  
        $ids = $this->request->request('ids');

        $Timelist=Timelist::where('id','in',$ids)->select();
        $list = ScheduleModel::where('user_id',$this->auth->id)
                        ->where('date',$date)
                        ->delete();         
        $data=[];
        foreach ($Timelist as $row) {
            $data[]=[
                'user_id'   =>  $this->auth->id,
                'date'      =>  $date,
                "rest_start_time"   =>$row['start_time'],
                "rest_timestamp"    =>strtotime($date." ".$row['start_time'])
            ];
        }
        $res = ScheduleModel::insertAll($data);
        $this->success('操作成功',[]);
    }    

    /**
     * @ApiTitle (获取上班时间)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.schedule/get_worktime)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'work_start_time':'string','work_end_time':'string','work_status':'integer'}", description="返回数据")
     */
    public function get_worktime()
    {
        $data = User::where('user_id',$this->auth->id)
                ->field('work_status,work_start_time,work_end_time')
                ->find();
        $this->success('查询成功',$data);
    }


    /**
     * @ApiTitle (设置上班时间)
     * @ApiMethod (Post)
     * @ApiRoute (/api/xiservice.schedule/set_worktime)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams   (name="work_start_time", type="string", description="开始工作时间", sample="09:00")
     * @ApiParams   (name="work_end_time", type="string", description="结束工作时间", sample="18:00")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'integer','order':'object','money':'float','createtime':'integer','createtime_text':'date'}", description="返回数据")
     */
    public function set_worktime()
    {
        $workStartTime = $this->request->post("work_start_time");
        $workEndTime = $this->request->post("work_end_time"); 
        $data['work_start_time'] = $workStartTime;
        $data['work_end_time'] = $workEndTime;
        $res =  User::where('user_id',$this->auth->id)
                    ->update($data);
        $this->success('保存成功',[]);
    }

    /**
     * @ApiTitle (设置接单状态)
     * @ApiMethod (GET)
     * @ApiRoute (/api/xiservice.schedule/set_workstatus)
     * @ApiHeaders (name=token, type=string, description="Token")
     * @ApiParams   (name="work_status", type="integer", description="工作状态:0=暂停接单,1=可接单", sample="1")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'id':'integer','order':'object','money':'float','createtime':'integer','createtime_text':'date'}", description="返回数据")
     */
    public function set_workstatus()
    {
        $workStatus = $this->request->request("work_status");
        $data['work_status'] = $workStatus;
        $res =  User::where('user_id',$this->auth->id)
                    ->update($data);
        $this->success('保存成功',[]);
    }

     /**
     * @ApiTitle    (获取订单服务人员时间列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/xiservice.schedule/ordertimelist)
     * @ApiHeaders  (name=token, type=string, description="Token")
     * @ApiParams   (name="date", type="date", description="日期")
     * @ApiParams   (name="worker_id", type="integer", description="服务人员id")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="返回数据")
     */
    public function ordertimelist()
    {
        $date = $this->request->request('date');  
        $orderId = $this->request->request('order_id'); 
        $order = orderModel::where('id',$orderId)->find();
        if(!$order){
            $this->error('订单信息错误!');
        } 
        $workerId = $order['worker_id'];
        $service  = ServiceModel::where('id',$order['service_id'])->find();
        $service_time_unit =  $service['service_time_unit'];
        if($service['service_time_type']==1||$service['service_time_type']==3){
            //单选
            $multiple = 0;
        }else{
            //多选
             $multiple = 1;
        }
        //判断30/60
        //单选多选
        //已选赋值
        if($workerId>0){
        
            $worker =  User::where('user_id',$workerId)->find();
            if(!$worker){
                $this->error('服务人员不存在!');
            } 
            if(!$worker['work_start_time']){
                $this->error('暂未设置工作时间!');
            }     
            $list = ScheduleModel::where('user_id',$workerId)
                            ->where('date',$date)
                            ->select();
                            
            $Timelist=Timelist::where('start_time','>=',$worker['work_start_time'])->where('end_time','<=',$worker['work_end_time'])->field(['*,0 as active'])->order('id asc')->select();

            $timearr =[];
            if($service_time_unit==30){
                if($Timelist){
                    foreach ($Timelist as $row) {
                        $row['checked'] = 1; //可选择
                        $row['date'] = $date;
                        $row['timestamp'] = strtotime($date." ".$row['start_time']);
                        $time = time()+3*60*60;
                        if($row['timestamp']<$time){
                             $row['checked'] = 0;
                        }
                        foreach ($list as $rows) {
                            if($row['start_time']==$rows['rest_start_time']){
                                 $row['checked'] = 0;
                            }
                        }

                        if($row['timestamp']>$time){
                             array_push($timearr,$row);
                        }
                    }
                }
            }else{
                if($Timelist){
                    foreach ($Timelist as $row) {
                        $row['checked'] = 1; //可选择
                        $row['date'] = $date;
                        $row['timestamp'] = strtotime($date." ".$row['start_time']);
                        $time = time()+3*60*60;

                        if($row['timestamp']<$time){
                             $row['checked'] = 0;
                        }
                        foreach ($list as $rows) {
                            if($row['start_time']==$rows['rest_start_time']){
                                 $row['checked'] = 0;
                            }
                        }

                        if($row['timestamp']>$time){
                            if(!strstr($row['start_time'], '30')){
                                array_push($timearr,$row);
                            }
                        }
                    }
                }
            }   


        }else{

            $Timelist=Timelist::where('start_time','>=','09:00:00')->where('end_time','<=','21:00:00')->field(['*,0 as active'])->order('id asc')->select();
            $timearr =[];
            if($service_time_unit==30){
                if($Timelist){
                    foreach ($Timelist as $row) {
                        $row['checked'] = 1; //可选择
                        $row['date'] = $date;
                        $row['timestamp'] = strtotime($date." ".$row['start_time']);
                        $time = time()+3*60*60;
                        if($row['timestamp']>$time){
                             array_push($timearr,$row);
                        }
                    }
                }
            }else{
                if($Timelist){
                    foreach ($Timelist as $row) {
                        $row['checked'] = 1; //可选择
                        $row['date'] = $date;
                        $row['timestamp'] = strtotime($date." ".$row['start_time']);
                        $time = time()+3*60*60;
                        if($row['timestamp']>$time){
                            if(!strstr($row['start_time'], '30')){
                                array_push($timearr,$row);
                            }
                        }
                    }
                }
            }         
        }

        $data['timelist'] = $Timelist;
        $data['multiple'] = $multiple;
        $this->success('查询成功',$data);        
    }
}